package com.wn26.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wn26.bean.User;
import com.wn26.service.UserService;
import com.wn26.service.impl.UserServiceImpl;

@WebServlet(name = "UserServlet", urlPatterns = { "/userLogin" })
public class UserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置编码格式
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");

		// --1.利用请求对象接受参数
		String user_name = req.getParameter("user_name");
		String user_pwd = req.getParameter("user_pwd");

		System.out.println(user_name + user_pwd);
		//--2.调用service层方法
		UserService us = new UserServiceImpl();
		try {
			User user = us.userLogin(user_name, user_pwd);
			if (user == null) {
				System.out.println("用户不存在");
				req.getRequestDispatcher("register.html").forward(req, resp);
			} else {
				req.getRequestDispatcher("index.html").forward(req, resp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
}
